<?php

if (!defined('BASEPATH'))
     exit('No direct script access allowed');

/**
 * This class is responsibile to find available: objects, rooms, rooms per object
 * 
 * Inputs are
 * 
 * duration of booking: Checkin, Checkout,
 * number of persons: Nr adults, Nr childrens
 * Destination or Hotel: 
 * 
 */
class search {

     private $checkin;
     private $checkout;
     private $nradults;
     private $nrchildrens;
     private $nrpersons;
     private $test_object_id;
     private $room_id;
     private $results;
     private $detinations_ids = NULL;
     private $hotels_ids = NULL;
     private $destination;
     private $hotel;
     private $destHot;
     private $childsage = array();
     private $search_objects;
     private $ignore_0_adults = FALSE;
     private $search_by_type;
     private $object_type;

     public function __construct() {

          $this->CI = &get_instance();
          $this->CI->load->model('search_m');
          $this->CI->load->model("destinations_model");
          $this->CI->load->model("objects_model");
          $this->CI->load->helper("percent");
          $this->CI->load->library('prices/discount_finder');
          $this->CI->load->helper("calculate_days");
     }

     /**
      * 
      * Show error message
      * @param $str error string
      */
     public function throwError($str) {
          echo $str;
          exit();
     }

     public function getCheckin() {
          return $this->checkin;
     }

     public function setCheckin($checkin) {
          $this->checkin = strtotime($checkin);
     }

     public function getCheckout() {
          return $this->checkout;
     }

     public function setCheckout($checkout) {
          $this->checkout = strtotime($checkout);
     }

     public function getNradults() {
          return $this->nradults;
     }

     public function setNradults($nradults) {
          $this->nradults = (int) $nradults;
     }

     public function getNrchildrens() {
          return $this->nrchildrens;
     }

     public function setNrchildrens($nrchildren) {
          $this->nrchildrens = (int) $nrchildren;
     }

     public function getNrpersons() {
          return $this->nrpersons;
     }

     public function setNrpersons($nrpersons) {
          $this->nrpersons = $nrpersons;
     }

     public function getTest_object_id() {
          return $this->test_object_id;
     }

     public function setTest_object_id($test_object_id) {
          $this->test_object_id = $test_object_id;
     }

     public function getRoom_id() {
          return $this->room_id;
     }

     public function setRoom_id($room_id) {
          $this->room_id = $room_id;
     }

     public function getResults() {
          return $this->results;
     }

     public function setResults($results) {
          $this->results = $results;
     }

     public function getDetinations_ids() {
          return $this->detinations_ids;
     }

     public function setDetinations_ids($detinations_ids) {
          $this->detinations_ids = $detinations_ids;
     }

     public function getHotels_ids() {
          return $this->hotels_ids;
     }

     public function setHotels_ids($hotels_ids) {
          $this->hotels_ids = $hotels_ids;
     }

     public function setDestination($destination) {
          $this->destination = $destination;
     }

     public function getDestination($destination) {
          return $this->destination;
     }

     public function setHotel($hotel) {
          $this->hotel = $hotel;
     }

     public function getHotel($hotel) {
          return $this->hotel;
     }

     public function getChildsAge() {
          return $this->childsage; //return array
     }

     public function setChildsAge($age) {
          if (count($this->childsage) + 1 > $this->nrchildrens)
               $this->throwError("Count of ages and childres mismach");
          array_push($this->childsage, (int) $age);
     }

     public function clearChildsAge() {
          $this->childsage = array();
     }

     public function getDestHot() {
          return $this->destHot;
     }

     public function setDestHot($destHot) {
          $this->destHot = $destHot;

          $this->setDestination($this->destHot);
          $destinations = $this->find_destinations_id();

          if (count($destinations) == 0) {
               $this->setDetinations_ids(NULL);
               $this->setHotel($this->destHot);
               $hotels = $this->find_hotels_id();
               if (count($hotels) == 0) {
                    $this->setHotels_ids(NULL);
               } else {
                    $this->setHotels_ids($hotels);
               }
          } else {
               $this->setDetinations_ids($destinations);
          }
     }

     public function getSearch_objects() {
          return $this->search_objects;
     }

     public function setSearch_objects($search_objects) {
          $this->search_objects = $search_objects;
     }

     public function getIgnore_0_adults() {
          return $this->ignore_0_adults;
     }

     public function setIgnore_0_adults($ignore_0_adults) {
          $this->ignore_0_adults = $ignore_0_adults;
     }

     public function getSearch_by_type() {
          return $this->search_by_type;
     }

     public function setSearch_by_type($search_by_type) {
          $this->search_by_type = $search_by_type;
     }

     public function getObject_type() {
          return $this->object_type;
     }

     public function setObject_type($object_type) {
          $this->object_type = $object_type;
     }

     // Max person problem
     // If person is child, and have discount 100%, it is not calculated as person in serch
     // So we have variated nr_persons in serch depending on room discount
     //
        // To find disocunts, we need room_ID and childsAge array
     // We can preform search with nr_adults as nr_persons, and go troug each record,
     // find dicount on room, increse nr_person for childs that are threated as person in serch,
     // Then simply remove rows that no mach max_person column
     public function preform_search() {

          // check if Adults person exists
          if (!isset($this->nradults) || !$this->nradults > 0):
               if (!$this->ignore_0_adults)
                    $this->throwError("Please add at least one Adlut person.");
          endif;


          // set nr persons and nr nights and row_price
          //$this->setNrpersons($this->nradults + $this->nrchildrens);
          // fix for discounts and childs free of charcge
          $this->setNrpersons($this->nradults);

          // find discounts if any and decrese nrpersons if discount is 100(percent %)
          //$this->set_nr_persons_id_discounts_100();

          if (!isset($this->room_id)):
               if ($this->hotels_ids == NULL && $this->detinations_ids == NULL) {
                    return array();
               }
          endif;

          // test search on one object if test_id exists
          if (isset($this->test_object_id)) {
               $this->results = $this->CI->search_m->test_seacrh(
                       $this->checkin, $this->checkout, $this->nrpersons, $this->detinations_ids, $this->hotels_ids, $this->test_object_id
               );
          } else if (isset($this->room_id)) {
               $this->results = $this->CI->search_m->test_seacrh(
                       $this->checkin, $this->checkout, $this->nrpersons, $this->detinations_ids, $this->hotels_ids, NULL, $this->room_id
               );
          } else if (isset($this->search_by_type)) {
               log_message("error", $this->object_type);
               $this->results = $this->CI->search_m->test_seacrh(
                       $this->checkin, $this->checkout, $this->nrpersons, $this->detinations_ids, $this->hotels_ids, NULL, NULL, TRUE, $this->object_type
               );
          } else if (isset($this->search_objects)) {

               $this->results = $this->CI->search_m->test_seacrh(
                       $this->checkin, $this->checkout, $this->nrpersons, $this->detinations_ids, $this->hotels_ids, NULL, NULL, TRUE
               );
          } else {
               $this->results = $this->CI->search_m->test_seacrh(
                       $this->checkin, $this->checkout, $this->nrpersons, $this->detinations_ids, $this->hotels_ids
               );
          }

          return $this->if_child_discount_calculate_as_person_unset_rows_depending_on_max_person($this->results);
     }

     public function find_destinations_id() {
          $destinations = $this->CI->destinations_model->find_by_name($this->destination);

          // find destinations ID
          $destinations_ID = array();
          if ($destinations) :

               foreach ($destinations as $one) :
                    $destinations_ID[] = $one['ID'];

                    // check if destination have children and include all children in search
                    if ($one['parent_ID'] == 0) :
                         $childrens = $this->CI->destinations_model->get_children($one['ID']);
                         foreach ($childrens as $child) :
                              $destinations_ID[] = $child['ID'];
                         endforeach;

                    endif;

               endforeach;
          endif;

          return $destinations_ID;
     }

     public function find_hotels_id() {
          $hotels = $this->CI->objects_model->find_by_name($this->hotel);

          // find hotels ID
          $hotels_ID = array();
          if ($hotels) :

               foreach ($hotels as $one) :
                    $hotels_ID[] = $one['object_ID'];

               endforeach;
          endif;

          return $hotels_ID;
     }

     /**
      * Must have inputs are:
      * 
      * ChildsAge:$this->childsage as array, 
      * Room ID: room_id
      *         
      */
     public function if_child_discount_calculate_as_person_unset_rows_depending_on_max_person($rooms) {

         // check if childage is set
            for($z=0; $z < $this->nrchildrens; $z++):
                if(!isset($this->childsage[$z]))
                    $this->throwError('Please specify childs age for each child');
            endfor;
         
          $cnt = count($rooms);

          if (isset($this->childsage) && count($this->childsage) > 0):
               for ($i = 0; $i < $cnt; $i++):
                    $max = $rooms[$i]['room_max_person'] - $this->nradults;
                    // Check if children in search
                    // if so, remove search rows that don't mach max person if child person is not free 100% discount

                    foreach ($this->childsage as $age) :
                         $this->CI->discount_finder->setRoom_id($rooms[$i]['room_ID']);
                         $this->CI->discount_finder->setChildAge($age);
                         $discount = (int) $this->CI->discount_finder->find_discount();
                         $free_of_charge = (int) 100;
                         if ($discount !== $free_of_charge):
                              $max-=1;
                              if ($max < 0):
                                   unset($rooms[$i]);
                                   break;
                              endif;
                         endif;

                    endforeach;

               endfor;
          endif;

          return array_values($rooms);
     }

}

?>
